require_relative "../Expr"

# Expression for bracket operators. Floor operators also count as bracket
# operators.
#
# @author Paul T.
class Calcula::Exprs::BracedExpr < Calcula::Expr

  # @param startTok [Calcula::Token] The starting token of this expression
  # @param endTok [Calcula::Token] The ending token of this expression
  # @param inner [Calcula::Expr]
  def initialize(startTok, endTok, inner)
    @startTok = startTok
    @endTok = endTok
    @inner = inner
  end

  # @see Calcula::Expr#to_s
  # @param (see Calcula::Expr#to_s)
  # @return (see Calcula::Expr#to_s)
  def to_s(form: :src)
    innerTxt = @inner.to_s(form: form)
    case form
    when :src then
      "#{@startTok.text}#{innerTxt}#{@endTok.text}"
    when :tree then
      "#{@startTok.text} #{innerTxt} #{@endTok.text}"
    when :ruby then
      case @startTok.id
      when :PAREN_O then
        "(#{innerTxt})"
      when :SQUARE_O then
        "(#{innerTxt}).floor.to_r"
      else
        raise "Unknown delimiter #{@startTok.id}"
      end
    else
      nil
    end
  end

  # @see Calcula::Expr#children
  # @param (see Calcula::Expr#children)
  # @return (see Calcula::Expr#children)
  def children
    [@inner]
  end
end
